<?php


  require('includes/application_top.php');

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  if (tep_not_null($action)) {
    switch ($action) {
      case 'insert':
      case 'save':
        if (isset($HTTP_GET_VARS['mID'])) $images_id = tep_db_prepare_input($HTTP_GET_VARS['mID']);
        $order_ = tep_db_prepare_input($HTTP_POST_VARS['order_']);
         $published = tep_db_prepare_input($HTTP_POST_VARS['published']);
         $link = tep_db_prepare_input($HTTP_POST_VARS['link']);
        $sql_data_array = array('order_' => $order_,'published'=>$published,'link'=>$link);
        if(!empty($_FILES['image']['name'])){
        if ($manufacturers_image = new upload('image', DIR_FS_CATALOG_IMAGES)) {
           include('resizeimages.php');
            $image = new SimpleImage();
            $image->load(DIR_FS_CATALOG_IMAGES.$manufacturers_image->filename);
           //$image->resize($thumwidth,$thumheight);
           $image->resizeToWidth(724);
           $filename=md5($manufacturers_image->filename.time()).'.'.end(explode(".", $manufacturers_image->filename));
            $image->save(DIR_FS_CATALOG_IMAGES.'slideshow_'.$filename);
           $sql_data_array['image']=$filename;//$manufacturers_image->filename;
           @unlink(DIR_FS_CATALOG_IMAGES.$manufacturers_image->filename);
          $sql_data_array['image']=$filename;
        }
        }
        if ($action == 'insert') {

          tep_db_perform('slideshow_images', $sql_data_array);
          $id = tep_db_insert_id();
        } elseif ($action == 'save') {
            if(!empty($sql_data_array['image']))
            tep_db_query("update slideshow_images set order_ = '" . $sql_data_array['order_'] . "', image = '" . $sql_data_array['image'] . "', link = '" . $sql_data_array['link'] . "', published='".$sql_data_array['published']."' where id = '" . (int)$images_id . "'");
            else
            tep_db_query("update slideshow_images set order_ = '" . $sql_data_array['order_'] . "', link = '" . $sql_data_array['link'] . "',published='".$sql_data_array['published']."' where id = '" . (int)$images_id . "'");
          //tep_db_perform('product_images', $sql_data_array, 'update', "id = '" . (int)$images_id . "'");
        }

        tep_redirect(tep_href_link('slideshow.php', (isset($HTTP_GET_VARS['page']) ? 'page=' . $HTTP_GET_VARS['page'] . '&' : '') . 'mID=' . $images_id.'&product_id='.@(int)$_GET['product_id']));
        break;
      case 'deleteconfirm':
        $manufacturers_id = tep_db_prepare_input($HTTP_GET_VARS['mID']);

          $manufacturer_query = tep_db_query("select image from slideshow_images where id = '" . (int)$manufacturers_id . "'");
          $manufacturer = tep_db_fetch_array($manufacturer_query);

          $image_location = DIR_FS_DOCUMENT_ROOT . DIR_WS_CATALOG_IMAGES . $manufacturer['image'];

          if (file_exists($image_location)) @unlink($image_location);
        

        tep_db_query("delete from slideshow_images where id = '" . (int)$manufacturers_id . "'");
      //  tep_db_query("delete from " . TABLE_MANUFACTURERS_INFO . " where manufacturers_id = '" . (int)$manufacturers_id . "'");


        tep_redirect(tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'].'&product_id='.@(int)$_GET['product_id']));
        break;
    }
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
         
            <td class="pageHeading">Danh sách hình ảnh của slideshow</td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent">Thumnail</td>
                <td class="dataTableHeadingContent">Trạng thái</td>
                <td class="dataTableHeadingContent" align="right"><?php echo 'action'; ?>&nbsp;</td>
              </tr>
<?php

    $manufacturers_query_raw = "select * from slideshow_images ";
  $manufacturers_query = tep_db_query($manufacturers_query_raw);
  while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
    if ((!isset($HTTP_GET_VARS['mID']) || (isset($HTTP_GET_VARS['mID']) && ($HTTP_GET_VARS['mID'] == $manufacturers['id']))) && !isset($mInfo) && (substr($action, 0, 3) != 'new')) {

      $mInfo = new objectInfo($manufacturers);
    }

    if (isset($mInfo) && is_object($mInfo) && ($manufacturers['id'] == $mInfo->id)) {
      echo '              <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $manufacturers['id'].'&product_id='.@(int)$_GET['product_id'] . '&action=edit') . '\'">' . "\n";
    } else {
      echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $manufacturers['id'].'&product_id='.@(int)$_GET['product_id']) . '\'">' . "\n";
    }
?>
                <td><img width="50" src="../images/<?php echo 'slideshow_'.$manufacturers['image'] ?>"  /></td>
                <td class="dataTableContent">
                <?php
                    if($manufacturers['published']==0){
                        echo "Chưa kích hoạt";
                    }else{
                        echo "Đã kích hoạt";    
                    }                           
                    ?>
                </td>
                <td class="dataTableContent" align="right"><?php if (isset($mInfo) && is_object($mInfo) && ($manufacturers['id'] == $mInfo->id)) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif'); } else { echo '<a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $manufacturers['id'].'&product_id='.@(int)$_GET['product_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
  }
  if (empty($action)) {
?>
              <tr>
                <td align="right" colspan="2" class="smallText"><?php echo '<a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id. '&product_id='.@(int)$_GET['product_id'].'&action=new') . '">' . tep_image_button('button_insert.gif', IMAGE_INSERT) . '</a>'; ?></td>
              </tr>
<?php
  }
?>
            </table></td>
<?php
  $heading = array();
  $contents = array();

  switch ($action) {
    case 'new':
      $heading[] = array('text' => '<b>Thêm mới hình ảnh</b>');

      $contents = array('form' => tep_draw_form('manufacturers', 'slideshow.php', 'action=insert', 'post', 'enctype="multipart/form-data"'));
      $contents[] = array('text' => '');
      $contents[] = array('text' => '<br>Thứ tự<br>' . tep_draw_input_field('order_', $mInfo->order_));
      $contents[] = array('text' => '<br>Hình ảnh<br>' . tep_draw_file_field('image'));
      $contents[] = array('text' => '<br>Liên kết<br>' . tep_draw_input_field('link', $mInfo->link));
      $contents[] = array('text' => '<br>' . 'Kích hoạt' . '<br>' . tep_draw_checkbox_field('published', '1', (($mInfo->published == '1') ? true : false)));  

      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_save.gif', IMAGE_SAVE) . ' <a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $HTTP_GET_VARS['mID']) . '&product_id='.@(int)$_GET['product_id'].'">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    case 'edit':
      $heading[] = array('text' => '<b>Sửa thông tin image</b>');

      $contents = array('form' => tep_draw_form('manufacturers', 'slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id . '&product_id='.@(int)$_GET['product_id'].'&action=save', 'post', 'enctype="multipart/form-data"'));
      $contents[] = array('text' => 'Sửa thông tin hình ảnh');
      $contents[] = array('text' => '<br>Thứ tự<br>' . tep_draw_input_field('order_', $mInfo->order_));
      $contents[] = array('text' => '<br>Hình ảnh<br>' . tep_draw_file_field('image') . '<br>' . $mInfo->image);
      $contents[] = array('text' => '<br>Liên kết<br>' . tep_draw_input_field('link', $mInfo->link));
      $contents[] = array('text' => '<br>Kích hoạt' . tep_draw_checkbox_field('published', '1', (($mInfo->published == '1') ? true : false))); 
     

      //$contents[] = array('text' => '<br>' . TEXT_MANUFACTURERS_URL . $manufacturer_inputs_string);
      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_save.gif', IMAGE_SAVE) . ' <a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->manufacturers_id.'&product_id='.@(int)$_GET['product_id']) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    case 'delete':
      $heading[] = array('text' => '<b</b>');

      $contents = array('form' => tep_draw_form('manufacturers', 'slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id . '&product_id='.@(int)$_GET['product_id'].'&action=deleteconfirm'));
      $contents[] = array('text' => 'Bạn có chắc chắn muốn xóa hình ảnh <b>' . $mInfo->title . '</b>?');

      
      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', IMAGE_DELETE) . ' <a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id.'&product_id='.@(int)$_GET['product_id']) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    default:
      if (isset($mInfo) && is_object($mInfo)) {
        $heading[] = array('text' => '<b>' . $mInfo->image. '</b>');
        $contents[]=array('text'=>'<br/><img width="200" src="../images/slideshow_'.$mInfo->image.'" /><br/>');
        $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id . '&product_id='.@(int)$_GET['product_id'].'&action=edit') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> <a href="' . tep_href_link('slideshow.php', 'page=' . $HTTP_GET_VARS['page'] . '&mID=' . $mInfo->id.'&product_id='.@(int)$_GET['product_id'] . '&action=delete') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a>');

       
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="25%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
         
        </table></td>
      </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
